﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_RelativeLinkEdit : System.Web.UI.Page
{
    private int RelativeLinkID;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {
            if (Session["OperationType"].ToString().Equals("New"))
            {
                if (0 == (int)Session["GroupID"])
                {
//                     PublishLabel.Visible = false;
//                     cbPublish.Visible = false;
                }
            }
            else
            {
                RelativeLinkID = Convert.ToInt32(Session["News_id"]);

                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var LinksInfos = from linksinfo in dc.LinksInfos
                        where linksinfo.LinksInfo_id.Equals(RelativeLinkID)
                           select new
                           {
                               标题 = linksinfo.Title,
                               内容 = linksinfo.Content,
                               发布 = linksinfo.isPublished
                           };

                foreach (var linksinfo in LinksInfos)
                {
                    tbLinkAddress.Text = Convert.ToString(linksinfo.内容);
                    tbLinkName.Text = Convert.ToString(linksinfo.标题);
//                    if (news.发布 == 1) cbPublish.Checked = true;
                }
                if (0 == (int)Session["GroupID"])
                {
//                     PublishLabel.Visible = false;
//                     cbPublish.Visible = false;
                }
// 
//                 var picure = from picture in dc.Pictures
//                              where picture.News_id.Equals(PictureNewsID)
//                              select picture.URL;
//                 foreach (var pic in picure)
//                 {
//                     labelUpload.Text = "已上传文件";
//                 }

                dc.Dispose();

            }
        }
    }
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        string tableTitle = "ctl00$ContentPlaceHolder$tbLinkName";
        string tableContent = "ctl00$ContentPlaceHolder$tbLinkAddress";
//        string tablePublish = "ctl00$ContentPlaceHolder$cbPublish";
        if (0 == (int)Session["GroupID"])
        {
//             PublishLabel.Visible = false;
//             cbPublish.Visible = false;
        }
        if (Session["OperationType"].ToString().Equals("New"))
        {

            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();

            LinksInfo linksinfo = new LinksInfo();
            linksinfo.Title = Request.Form[tableTitle];
            linksinfo.Content = Request.Form[tableContent];
            linksinfo.isPublished = 1;
            try
            {
                dc.LinksInfos.InsertOnSubmit(linksinfo);
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            dc.Dispose();
        }
        else if (Session["OperationType"].ToString().Equals("Edit"))
        {
            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
            var LinksInfos = from linkinfo in dc.LinksInfos
                       where linkinfo.LinksInfo_id.Equals(RelativeLinkID)
                       select linkinfo;
            foreach (var linkinfo in LinksInfos)
            {
                linkinfo.Title = Request.Form[tableTitle];
                linkinfo.Content = Request.Form[tableContent];

//                 if (Request.Form[tablePublish] != null)
//                 {
//                     news.isPublished = 1;
//                     if (news.Checker == 0) news.Checker = (int)Session["UserID"];
//                 }
//                 else { news.isPublished = 0; news.Checker = 0; }
            }
//             title.Value = Request.Form[tableTitle];
//             content.Value = Request.Form[tableContent];
//             if (Request.Form[tablePublish] != null) cbPublish.Checked = true;
//             else cbPublish.Checked = false;
            dc.SubmitChanges();
            dc.Dispose();

        }
        Response.Redirect("../Admin/AdminHome.aspx");
    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
     }
}
